package com.dp.struts2.yc.channel;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dp.pojo.News;
import com.dp.pojo.NewsVo;
import com.dp.pojo.PageCtrl;
import com.dp.pub.BasePageAction;
import com.dp.pub.SysProperty;
import com.dp.service.jdc.ChannelServiceImpl3;
import com.dp.service.jdc.IndexServiceImpl;
import com.dp.service.jdc.NewsServiceImpl;
import com.dp.vo.TChannels;
/**
 * 
 * 
 */
/**
 * @author Administrator
 *
 */
@Controller
@Scope("session")
public class YcchannelAction extends BasePageAction implements ServletContextAware {
    /**
     * 
     */
    
    private static Logger logger = Logger.getLogger(YcchannelAction.class);
    /**
     * 栏目新闻列表
     */
    private List<News> newsList;
    
    //private List<TChannels> channelNameList;
    private List<TChannels> navChannels ;
    private List<List<TChannels>> nav2Channels;
    private String navOnChannelId;
    
    
    private List<TChannels> channelsList;
    
    /**
     *新闻所属的上一级栏目 
     */
    private TChannels newsTopChannel;

    /**
     *新闻所属的最顶级栏目
     */
    private TChannels leftTopChannel;
    
    //分页数据
    private PageCtrl pageCtrl;
    private int pageSize = 10;
    private int pageNo = 0;
    
    @Autowired
    private IndexServiceImpl indexService;
    @Autowired
    private ChannelServiceImpl3 channelService;
    @Autowired
    private NewsServiceImpl newsService;
    
    private String channelId ;
    private int rid;
    private NewsVo news;//新闻内容
    
    /**普通链接channelId是末级栏目的
     * @return
     */
    public String channel(){
        navChannels = indexService.getChannelsList(SysProperty.getPrep("hdnews_rootch"));
        nav2Channels = new ArrayList<List<TChannels>>();
        for (TChannels t : navChannels) {
            List<TChannels> temp = channelService.getLastChannels(t.getChannelId());
            nav2Channels.add(temp);
        }
        channelsList = channelService.getChannels(channelId);
        newsTopChannel = channelService.getChannelById(channelId);
        leftTopChannel = channelService.getChannelById(newsTopChannel.getPreviousId());
        navOnChannelId=leftTopChannel.getChannelId();
        newsList = channelService.getNews(channelId,pageSize,pageNo);
        
        pageCtrl = channelService.getPageCtrl(channelId, pageNo, pageSize);
        
        
        if(newsTopChannel.getChannelType().equals("image")){
            newsList = channelService.getNews(channelId,8,pageNo);
            
            pageCtrl = channelService.getPageCtrl(channelId, pageNo, 8);
            for (News news : newsList) {
                news.setResourceRid(indexService.getImgRid(news.getCId()));
            }
               return "ycimgchannel";
           }
        newsList = channelService.getNews(channelId,pageSize,pageNo);
        
        pageCtrl = channelService.getPageCtrl(channelId, pageNo, pageSize);
        if(pageCtrl.getResultCount()==1){
            news = newsService.getNewsByContentid(newsList.get(0).getContentId());
            news = newsService.conversionHtml(news);
            return "ycnews";   
        }
           return "ycchannel";
    }
    /**点击一级导航栏的过来的链接channelID
     * @return
     */
    public String navchannel(){
        navChannels = indexService.getChannelsList(SysProperty.getPrep("hdnews_rootch"));
        nav2Channels = new ArrayList<List<TChannels>>();
        for (TChannels t : navChannels) {
            List<TChannels> temp = channelService.getLastChannels(t.getChannelId());
            nav2Channels.add(temp);
        }
        channelsList = channelService.getLastChannels(channelId);

        leftTopChannel = channelService.getChannelById(channelId);
        newsTopChannel = channelService.getChannelById(channelsList.get(0).getChannelId());
        navOnChannelId=leftTopChannel.getChannelId();
        
        if(newsTopChannel.getChannelType().equals("image")){
            pageCtrl = channelService.getPageCtrl(channelsList.get(0).getChannelId(), pageNo, 8);
            newsList = channelService.getNews(channelsList.get(0).getChannelId(),8,pageNo);
            for (News news : newsList) {
                news.setResourceRid(indexService.getImgRid(news.getCId()));
            }
            return "ycimgchannel";
        }
        pageCtrl = channelService.getPageCtrl(channelsList.get(0).getChannelId(), pageNo, pageSize);
        newsList = channelService.getNews(channelsList.get(0).getChannelId(),pageSize,pageNo);
        if(newsList.size()==1){
            news = newsService.getNewsByContentid(newsList.get(0).getContentId());
            news = newsService.conversionHtml(news);
            return "ycnews";   
        }
        return "ycchannel";
    }
    /**
     * 根据图片rid给img加图片
     */
    public void image(){
        response.setContentType("image/jpeg");
        response.setHeader("Cache-Control", "no-cache");
        
        String fullpath=indexService.getfullpath(rid);
        fullpath = indexService.convertFullPath(fullpath);
            try {
                InputStream is = new FileInputStream(fullpath);
                OutputStream fos = response.getOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) > 0)   {   
                    fos.write(buffer , 0 , len);   
                }
                response.flushBuffer();
                is.close();
                fos.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        
        
       
   
    @Override
    public void setServletContext(ServletContext arg0) {
        // TODO Auto-generated method stub
        
    }
    public List<News> getNewsList() {
        return newsList;
    }
    public void setNewsList(List<News> newsList) {
        this.newsList = newsList;
    }
    public String getChannelId() {
        return channelId;
    }
    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }
    public List<TChannels> getNavChannels() {
        return navChannels;
    }
    public void setNavChannels(List<TChannels> navChannels) {
        this.navChannels = navChannels;
    }
    public List<TChannels> getChannelsList() {
        return channelsList;
    }
    public void setChannelsList(List<TChannels> channelsList) {
        this.channelsList = channelsList;
    }
    public PageCtrl getPageCtrl() {
        return pageCtrl;
    }
    public void setPageCtrl(PageCtrl pageCtrl) {
        this.pageCtrl = pageCtrl;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getPageNo() {
        return pageNo;
    }
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }
    public int getRid() {
        return rid;
    }
    public void setRid(int rid) {
        this.rid = rid;
    }
    public NewsVo getNews() {
        return news;
    }
    public void setNews(NewsVo news) {
        this.news = news;
    }
    public TChannels getLeftTopChannel() {
        return leftTopChannel;
    }
    public void setLeftTopChannel(TChannels leftTopChannel) {
        this.leftTopChannel = leftTopChannel;
    }
    public TChannels getNewsTopChannel() {
        return newsTopChannel;
    }
    public void setNewsTopChannel(TChannels newsTopChannel) {
        this.newsTopChannel = newsTopChannel;
    }
    public List<List<TChannels>> getNav2Channels() {
        return nav2Channels;
    }
    public void setNav2Channels(List<List<TChannels>> nav2Channels) {
        this.nav2Channels = nav2Channels;
    }
    public String getNavOnChannelId() {
        return navOnChannelId;
    }
    public void setNavOnChannelId(String navOnChannelId) {
        this.navOnChannelId = navOnChannelId;
    }
}
